// pages/index/detail/packet/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
	data: {
		images: {},
		share_activity_logs_id: '', // 活动记录id
		imgurl: app.urlimg,
		money: '',
		title: '', // 分享活动的名称
		// 红包金额
		amount_lists: ['', '', '', '', '', '', '',],
		amount: '',
		people: '',
		//规则说明
		rule_text: '',
		// 规则框
		rule: false,
		//猜你喜欢
		likegoods: [],
	},
	//查看我的红包
	click: function () {
		wx.redirectTo({
			url: '/pages/index/detail/packet_securities/index?id=' + this.data.options.id,
		})
	},
	//人数已满
	num_all: function () {
		//提示信息
		wx.showToast({
			title: '人数已满',
			image: '/pages/icon/delete.png',  //自定义图标的本地路径，image 的优先级高于 icon  
			duration: 1200,
		})
	},
	// 查看规则
	rule_click: function () {
		this.setData({
			rule: !this.data.rule,
		})
	},
	// 广告图片大小（banner）
	imageLoad: function (e) {
		var $width = e.detail.width,    //获取图片真实宽度
			$height = e.detail.height,
			ratio = $width / $height;    //图片的真实宽高比例
		var viewWidth = 718,           //设置图片显示宽度，左右留有16rpx边距
			viewHeight = 718 / ratio;    //计算的高度值
		var image = this.data.images;
		//将图片的datadata-index作为image对象的key,然后存储图片的宽高值
		image[e.target.dataset.index] = {
			width: viewWidth,
			height: viewHeight
		}
		this.setData({
			images: image
		})
	},
	// 广告图片（banner）
	swiperItem: function (e) {
		var fid = e.currentTarget.id
		if (e.currentTarget.dataset.url !== '') {
			wx.redirectTo({
				url: e.currentTarget.dataset.url,
			})
		}
	},
  /**
   * 生命周期函数--监听页面加载
   */
	onLoad: function (options) {
		var that = this;
		var token_key = wx.getStorageSync('token_key');
		console.log(token_key);
		if (token_key == undefined || token_key == '') {
			app.getUserInfo(function () {
				load_fn();
			})
		} else {
			load_fn()
		}
		function load_fn() {
			console.log(options);
			wx.showShareMenu({
				withShareTicket: true,
			});
			wx.request({
				url: app.url + 'api_share/share_info?id=' + options.id,
				data: {},
				header: {
					'content-type': 'application/json',
					'token': wx.getStorageSync('token_key')
				},

				success: function (res) {
					console.log(res);
					var intro = res.data.result.intro;
					var all = [];
					for (var i = 0; i < 4; i++) {
						if (i < intro.length) {
							all.push(intro[i])
						} else {
							all.push({ url: '', name: '', }, )
						}
					}

					var amount = res.data.result.publish_bonus;
					var amount_lists = that.data.amount_lists;
					var amount_all = [];
					for (var i = 0; i < amount.length; i++) {
						amount_all.push(amount[i])
					}
					if (amount_all.length < amount_lists.length) {
						var num = amount_lists.length - amount_all.length;
					}
					for (var i = 0; i < num; i++) {
						amount_all.splice(0, 0, "0")
					}

					var hongbao = false;
					if (res.data.result.state == 2) {
						wx.request({
							url: app.url + 'api_share/get_luck_money/id/' + res.data.result.share_activity_logs_id, // 分享后点击页面链接,
							header: {
								'content-type': 'application/json',
								'token': wx.getStorageSync('token_key')
							},
							success: function (json) {
								console.log(json);
								that.setData({
									money: json.data.result.money,
								})
							}
						})
						hongbao = true;
					};
          
					that.setData({
						title: res.data.result.name, // 活动名称
						rule_text: res.data.result.share_info, // 规则说明
						amount_lists: amount_all,
						people: all, // 参加用户
						hongbao: hongbao,
						state: res.data.result.state,
						options: options,
						top_img: res.data.result.top_img,
						goods_list: res.data.result.goods_list,
						goods_nav_img: res.data.result.goods_nav_img,
            likegoods: (res.data.result.likegoods ? res.data.result.likegoods: []),
            ad_top: (res.data.result.ad_top ? res.data.result.ad_top : []),
					})
					console.log(that.data.goods_list);
				}
			})
		}
	},
	//关闭红包
	dele_hongbao: function () {
		this.setData({
			hongbao: false,
		})
	},
	// 猜你喜欢板块被点击
	like_click: function (e) {
		var that = this;
		console.log("猜你喜欢板块被点击，跳转到id对应的商品详情");
		var goods_id = e.currentTarget.dataset.goods_id;
		var index = e.currentTarget.dataset.index;
		console.log(that.data.goods_list);
		//判断进入的商品详情页面
		var url = "";
		console.log(that.data.goods_list[index].group_type);
		if (that.data.goods_list[index].group_type != '' && that.data.goods_list[index].group_type != undefined) {
			url = '/pages/group/groupdetail/index?id=' + goods_id + '&regiment_id=' + that.data.goods_list[index].regiment_id + "&group_type=" + that.data.goods_list[index].group_type;
		} else {
			url = '/pages/index/detail/index?id=' + goods_id;
		}

		console.log(url);
		wx.redirectTo({
			url: url,
		})
	},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
	onReady: function () {
		var that = this;


	},

  /**
   * 生命周期函数--监听页面显示
   */
	onShow: function () {
		var that = this;

		var i = 0;
		setInterval(function () {
			++i;
			var animation = wx.createAnimation({
				duration: 200,
			})
			that.animation = animation
			if (i % 2 == 0) {
				animation.scale(1, 1).step()
			} else {
				animation.scale(1.05, 1.05).step()
			}
			that.setData({
				animationData: animation.export(),
			})
		}, 350)
		var animation = wx.createAnimation({
			duration: 200,
		})

		that.animation = animation;

		animation.scale(1, 1).step()

		that.setData({
			animationData: animation.export(),
		})
	},

  /**
   * 生命周期函数--监听页面隐藏
   */
	onHide: function () {

	},

  /**
   * 生命周期函数--监听页面卸载
   */
	onUnload: function () {

	},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
	onPullDownRefresh: function () {

	},

  /**
   * 页面上拉触底事件的处理函数
   */
	onReachBottom: function () {

	},

  /**
   * 用户点击右上角分享
   */
	onShareAppMessage: function () {
		var that = this;
		console.log(that.data.options);
		return {
			title: that.data.title, // 标题
			path: '/pages/index/detail/packet/index?id=' + that.data.options.id, // 分享后点击页面链接
			success: function (res) {
				// console.log(res);
				wx.getShareInfo({
					shareTicket: res.shareTickets[0],
					success: function (res) {
						wx.login({
							success: function (login_res) {
								console.log(login_res);
								// 获取分享群id
								wx.request({
									url: app.url + 'api_share/get_shareopenGId/',
									data: {
										'id': that.data.options.id,
										'code': login_res.code,
										'encryptedData': res.encryptedData,
										'iv': res.iv,
									},
									header: {
										'content-type': 'application/json',
										'token': wx.getStorageSync('token_key')
									},

									success: function (res) {
										if (res.data.message == "该群已经转发过") {
											wx.showToast({
												title: '该群已经转发过',
												image: '/pages/icon/delete.png',
												duration: 1200,
											})
										} else {
											that.onLoad(that.data.options);
										}
									}
								})
							}
						})
					}
				});
			},
			fail: function (res) {
				// 转发失败
				console.log('转发失败');
			},
			complete: function (res) {
				// 转发结束的回调函数（转发成功、失败都会执行
				console.log(res)
			}
		}
	}
})